//
//  TZWMainTableViewController.swift
//  Sinaweibotzw
//
//  Created by 檀志文 on 16/4/8.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

/*
1.定义闭包(准备一段代码)
2.传递
3.调用
注意:顺序无所谓
*/

import UIKit

class TZWMainTableViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let mainTabBar = TZWMainTabBar()
        
//         3 定义闭包并设置
        mainTabBar.composeClosure = {() -> Void in
            print("加号按钮被点击了")
        }
        self.setValue(mainTabBar, forKey: "tabBar")
        
        setupChildViewControllers()
    }
    
        private func setupChildViewControllers() {
            let homeVc = TZWHomeTableViewController()
         setupChildViewController(homeVc, imageName: "tabbar_home", title: "首页")
            let mesVc = TZWMessageTableViewController()
            setupChildViewController(mesVc, imageName: "tabbar_message_center", title: "消息")
            let discoverVc = TZWDiscoverTableViewController()
            setupChildViewController(discoverVc, imageName: "tabbar_discover", title: "发现")
            let profileVc = TZWProfileTableViewController()
            setupChildViewController(profileVc, imageName: "tabbar_profile", title: "我")
        }
        
        private func setupChildViewController(controller: UIViewController, imageName:String, title: String){
            
//            设置tabbar 的图片
            controller.tabBarItem.image = UIImage(named:imageName)
            
            controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            controller.title =  title
            controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
            
            addChildViewController(UINavigationController(rootViewController: controller))
            
        }

}
